package com.ceres.farm.controller;



import com.github.pagehelper.Page;
import com.ceres.entity.PageResult;
import com.ceres.entity.Result;
import com.ceres.entity.StatusCode;
import com.ceres.farm.service.AreaService;
import com.ceres.farm.pojo.CustFarm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.User;


@RestController
@RequestMapping("/area")
public class AreaController {

    @Autowired
    AreaService areaService;

    /**
     * 农场片区的添加
     * @param custFarm
     * @return
     */
    @PostMapping("/add")
    public Result areaAdd(@RequestBody CustFarm custFarm){

        try {
            areaService.areaAdd(custFarm);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
        return new Result(true, StatusCode.OK);
    }

    /**'
     * 农场片区的修改
     * @param custFarm
     * @return
     */
    @PutMapping("/update")
    public Result areaUpdate(@RequestBody CustFarm custFarm){
        try {
        areaService.areaUpdate(custFarm);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
        return new Result(true, StatusCode.OK);
    }


    @GetMapping("/findArea")
    public Result findArea( String pageSize,String pageNumber,Integer custid,String name){
                Integer size=10;
                Integer number=1;
            if(StringUtils.isEmpty(pageSize)){
                try {
                    size = Integer.parseInt(pageSize);
                } catch (NumberFormatException e) {

                }
            }
            if( StringUtils.isEmpty(pageNumber)){
                try {
                    number = Integer.parseInt(pageNumber);
                } catch (NumberFormatException e) {

                }
            }



        try {
       Page<CustFarm> pageList= areaService.findPage(number,size,custid,name);
            PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
            return new Result(true,StatusCode.OK,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }

    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        try {
            CustFarm  custFarm=areaService.findById(id);
            return new Result(true,StatusCode.OK,custFarm);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }

    }
}
